package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.adpter.HotUserlastAdpter;
import com.lanou.dllo.findfood.adpter.HotUsertopAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.dialog.WebProgressDialog;
import com.lanou.dllo.findfood.entity.UserTop_by_city;
import com.lanou.dllo.findfood.entity.User_result;
import com.lanou.dllo.findfood.entity.User_top_Json;
import com.lanou.dllo.findfood.entity.User_top_all;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/22.
 */
public class HotUserActivity extends Activity{
    private  RecyclerView topRecylerView,bottomRecylerView;
    private RequestQueue queue;
    private User_result user_result;
    private HotUsertopAdpter topAdpter;
    private HotUserlastAdpter lastAdpter;
    private RecyclerViewHeader viewHeader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        initView();
        initData();

    }
 public  void    initView(){

     topRecylerView = (RecyclerView) findViewById(R.id.topRecylerView);
     GridLayoutManager manager = new GridLayoutManager(this,1);
     //, LinearLayout.HORIZONTAL,false
     topRecylerView.setLayoutManager(manager);
//     SpacesItemDecoration decoration = new SpacesItemDecoration(15);
//     topRecylerView.addItemDecoration(decoration);
     //绑定适配器
     topAdpter = new HotUsertopAdpter(this);
     topRecylerView.setAdapter(topAdpter);



     viewHeader = (RecyclerViewHeader) findViewById(R.id.header);

     bottomRecylerView = (RecyclerView) findViewById(R.id.bottomRecylerView);
     GridLayoutManager m = new GridLayoutManager(this,1);
     topRecylerView.setLayoutManager(manager);

     bottomRecylerView.setLayoutManager(m);
     lastAdpter = new HotUserlastAdpter(this);

     viewHeader.attachTo(bottomRecylerView,true);

     bottomRecylerView.setAdapter(lastAdpter);

    }

   private void initData(){
       final WebProgressDialog dialog = new WebProgressDialog(this,R.id.animaImgweb);
       dialog.show();

       String url = "http://api.meishixing.com/user/user/top/city_id=7&session_id=00001227993a5661b25da19c79c2a7c9a6affc";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
            //    Log.i("lanou"," " + response);
                Gson gson = new Gson();
                User_top_Json json = gson.fromJson(response,User_top_Json.class);
                if (json!= null){
                    User_result result = json.getResult();
                    if (result != null){
                        List<UserTop_by_city> cities = result.getTop_user_by_city();
                        if (cities != null && cities.size() > 0){

                            //添加到适配器
                           topAdpter.addData(cities);
                        }
                        List<User_top_all> allList = result.getTop_user_all();
                        if (allList != null && allList.size()>0){
                            //添加到下面头像的适配器
                            lastAdpter.addData(allList);
                        }
                        dialog.dismiss();
                    }

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
       VolleySington.getInstance().addRequest(request);
    }


    }


